package interactiond

import (
	"time"

	cinteractiond "gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/interactiond"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

// groupTotal 互动组互动统计值对象
type groupTotal struct {
	ITCode     string  `bson:"itCode"`     // 互动类型
	Total      int     `bson:"total"`      // 数量
	TotalScore int     `bson:"totalScore"` // 总分数
	AvgScore   float64 `bson:"avgScore"`   // 平均分数
}

func (t groupTotal) To() cinteractiond.GroupTotal {
	return cinteractiond.GroupTotal{
		ITCode:     t.ITCode,
		Total:      t.Total,
		TotalScore: t.TotalScore,
		AvgScore:   t.AvgScore,
	}
}
func (t *groupTotal) Full(g *cinteractiond.GroupTotal) {
	t.ITCode = g.ITCode
	t.Total = g.Total
	t.TotalScore = g.TotalScore
	t.AvgScore = g.AvgScore
}

type groupTotals []groupTotal

func (t groupTotals) To() (out []cinteractiond.GroupTotal) {
	out = make([]cinteractiond.GroupTotal, 0, len(t))
	for _, v := range t {
		out = append(out, v.To())
	}
	return
}
func (t *groupTotals) Full(g []cinteractiond.GroupTotal) {
	*t = make([]groupTotal, 0, len(g))
	for _, v := range g {
		nt := &groupTotal{}
		nt.Full(&v)
		*t = append(*t, *nt)
	}
}

// @collname 互动组集合(i_intGroup)
type collGroup struct {
	GroupID      string                     `bson:"_id" `         // 组唯一标识
	TargetID     string                     `bson:"targetID"`     // 互动目标ID
	TargetType   cinteractiond.IGTargetType `bson:"targetType"`   // 互动目标类型
	CreTime      time.Time                  `bson:"creTime"`      // 创建时间
	LastTime     time.Time                  `bson:"lastTime"`     // 子评论最后评论时间
	LastReplayer string                     `bson:"lastReplayer"` // 子评论最后评论人
	ReplayNum    int                        `bson:"replayNum"`    // 子评论数量
	Status       cinteractiond.IGStatus     `bson:"status"`       // 状态
	IsReplay     int                        `bson:"isReplay"`     // 是否允许发评论 0 不允许发评论<br/>1允许登录用户匿名发评论<br/>2 登录用户必须实名发评论（使用系统昵称）
	IsReplayChk  int                        `bson:"isReplayChk"`  // 发评论审核模式 0 不需要审核  <br/>1 需要所有者审核 <br/>2 需要组织审核 <br/>3 需要平台审核
	AllowCodes   []string                   `bson:"allowCodes"`   // 是否允许互动模式
	Totals       groupTotals                `bson:"totals"`       // 互动统计信息
}

func (c collGroup) CollID() string {
	return c.GroupID
}
func (collGroup) CollName() string {
	return collNameGroup
}
func (c collGroup) IsCache() bool {
	return c.GroupID != "" && c.Status == cinteractiond.IGStatusNormal
}

func (c collGroup) CacheExpire() time.Duration {
	return 60 * time.Second
}
func (c *collGroup) FullDesc(e cinteractiond.GroupDesc) {
	c.TargetID = e.TargetID
	c.TargetType = e.TargetType
	c.CreTime = e.CreTime

}
func (c *collGroup) FullTotals(gts []cinteractiond.GroupTotal) {
	c.Totals.Full(gts)

}
func (c collGroup) ToTotals() (gts []cinteractiond.GroupTotal) {
	return c.Totals.To()

}
func (c collGroup) ToDesc() cinteractiond.GroupDesc {
	return cinteractiond.GroupDesc{
		TargetID:   c.TargetID,
		TargetType: c.TargetType,
		CreTime:    c.CreTime,
	}
}

func (c *collGroup) FullReplayTotal(e cinteractiond.ChildReplyTotal) {
	c.LastTime = e.LastTime
	c.LastReplayer = e.LastReplayer
	c.ReplayNum = e.ReplayNum

}
func (c collGroup) ToReplayTotal() cinteractiond.ChildReplyTotal {
	return cinteractiond.ChildReplyTotal{
		LastTime:     c.LastTime,
		LastReplayer: c.LastReplayer,
		ReplayNum:    c.ReplayNum,
	}
}

func (c *collGroup) FullCtrl(e cinteractiond.GroupCtrl) {
	c.Status = e.Status
	c.IsReplay = e.IsReplay
	c.IsReplayChk = e.IsReplayChk
	c.AllowCodes = e.AllowCodes

}
func (c collGroup) ToCtrl() cinteractiond.GroupCtrl {
	return cinteractiond.GroupCtrl{
		Status:      c.Status,
		IsReplay:    c.IsReplay,
		IsReplayChk: c.IsReplayChk,
		AllowCodes:  c.AllowCodes,
	}
}

func (c collGroup) ToAggregate() ddd.Aggregate {
	return c.ToGroupAggregate()
}
func (c collGroup) ToGroupAggregate() *cinteractiond.GroupAggregate {
	return &cinteractiond.GroupAggregate{
		IGID:            c.GroupID,
		GroupDesc:       c.ToDesc(),
		ChildReplyTotal: c.ToReplayTotal(),
		GroupCtrl:       c.ToCtrl(),
		GroupTotal:      c.ToTotals(),
	}
}
